﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using AliCMS;
using System.Web;

namespace AliCMS.CourseModule.OleDb
{
	public class Category : OleDbBase, Dal.ICategory
	{
		public int GetInnerIDFromGuid(Guid guid)
		{
			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.ID, MapType(Alias.Category.ID)).Value = guid;
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.InnerID) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.ID) + "=?";
			object result = Command.ExecuteScalar();
			if (result == null)
				return 0;
			else
				return (int)result;
		}
		public Guid GetGuidFromInnerID(object id)
		{
			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = id;
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.ID) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			object result = Command.ExecuteScalar();
			if (result == null)
				return Guid.Empty;
			else
				return (Guid)result;
		}
		public Dictionary<string, object> Get(object id)
		{
			Dictionary<string, object> result = new Dictionary<string, object>();

			StringBuilder sql = new StringBuilder("SELECT TOP 1");
			sql.Append(" " + MapColumn(Alias.Category.ID));
			sql.Append("," + MapColumn(Alias.Category.InnerID));
			sql.Append("," + MapColumn(Alias.Category.Name, Lang));
			sql.Append("," + MapColumn(Alias.Category.ParentID));
			sql.Append("," + MapColumn(Alias.Category.ParentChain));
			sql.Append("," + MapColumn(Alias.Category.Picture));
			sql.Append("," + MapColumn(Alias.Category.Recommend));
			sql.Append("," + MapColumn(Alias.Category.IsDisplay));
			sql.Append("," + MapColumn(Alias.Category.Sort));
			sql.Append(" FROM " + MapTable(Alias.Objects.Category));

			if (id is Guid)
			{
				sql.Append(" WHERE " + MapColumn(Alias.Category.ID) + "=?");

				Command.Parameters.Clear();
				Command.Parameters.Add("@" + Alias.Category.ID, MapType(Alias.Category.ID)).Value = id;
			}
			else
			{
				sql.Append(" WHERE " + MapColumn(Alias.Category.InnerID) + "=?");

				Command.Parameters.Clear();
				Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = id;
			}

			Command.CommandText = sql.ToString();
			DataReader = Command.ExecuteReader();
			if (!DataReader.HasRows)
			{
				DataReader.Close();
				return null;
			}

			DataReader.Read();
			int i = 0;
			result[Alias.Category.ID] = DataReader[i++];
			result[Alias.Category.InnerID] = DataReader[i++];
			result[Alias.Category.Name] = DataReader[i++];
			result[Alias.Category.ParentID] = DataReader[i] == DBNull.Value ? Guid.Empty : DataReader[i]; i++;
			result[Alias.Category.ParentChain] = DataReader[i++];
			result[Alias.Category.Picture] = DataReader[i++];
			result[Alias.Category.Recommend] = DataReader[i++];
			result[Alias.Category.IsDisplay] = DataReader[i++];
			result[Alias.Category.Sort] = DataReader[i++];
			DataReader.Close();

			#region 权限
			if (CheckRight)
			{
				Dictionary<string, string> user = Manager.User;
				if (user != null)
				{
					#region 管理员
					if (user["Namespace"] == "AliCMS.SystemModule")
					{
					}
					#endregion
					#region 用户
					else
					{
					}
					#endregion
				}
			}
			#endregion

			return result;
		}
		public void Add(Dictionary<string, object> item)
		{
			Command.Parameters.Clear();

			#region 排序
			if (!item.ContainsKey(Alias.Category.Sort))
			{
				Command.CommandText = "SELECT MAX(" + MapColumn(Alias.Category.Sort) + ") + 1 FROM " + MapTable(Alias.Objects.Category);
				object sort = Command.ExecuteScalar();
				if (sort == DBNull.Value)
					item.Add(Alias.Category.Sort, 1);
				else
					item.Add(Alias.Category.Sort, sort);
			}
			#endregion

			#region 设置RowLang
			item.Add(Alias.Category.RowLang, "|" + Lang + "|");
			#endregion

			#region 设置ParentChain
			if (item.ContainsKey(Alias.Category.ParentID))
			{
				if (item[Alias.Category.ParentID] == DBNull.Value)
				{
					item[Alias.Category.ParentChain] = "0";
				}
				else
				{
					Command.Parameters.Clear();
					Command.Parameters.Add("@" + Alias.Category.ID, MapType(Alias.Category.ID));

					string parentChain = "";
					object parentID = item[Alias.Category.ParentID];
					object innerID;
					while (!Convert.IsDBNull(parentID))
					{
						Command.CommandText = "SELECT TOP 1 " + MapColumn(Alias.Category.ParentID) + "," + MapColumn(Alias.Category.InnerID) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.ID) + "=?";
						Command.Parameters["@" + Alias.Category.ID].Value = parentID;
						DataReader = Command.ExecuteReader();
						if (!DataReader.HasRows)
						{
							DataReader.Close();
							break;
						}

						DataReader.Read();
						parentID = DataReader[0];
						innerID = DataReader[1];
						DataReader.Close();

						parentChain = "." + innerID + parentChain;
					}

					item[Alias.Category.ParentChain] = "0" + parentChain;
				}
			}
			else
			{
				item[Alias.Category.ParentID] = DBNull.Value;
				item[Alias.Category.ParentChain] = "0";
			}
			#endregion

			#region 推荐
			if (item.ContainsKey(Alias.Category.IsRecommended))
			{
				if (Convert.ToBoolean(item[Alias.Category.IsRecommended]))
				{
					Command.CommandText = "SELECT MAX(" + MapColumn(Alias.Category.Recommend) + ") + 1 FROM " + MapTable(Alias.Objects.Category);
					object recommend = Command.ExecuteScalar();
					if (recommend == DBNull.Value)
						item[Alias.Category.Recommend] = 1;
					else
						item[Alias.Category.Recommend] = recommend;
				}
				else
				{
					item[Alias.Category.Recommend] = 0;
				}
				item.Remove(Alias.Category.IsRecommended);
			}
			#endregion

			StringBuilder names = new StringBuilder();
			StringBuilder values = new StringBuilder();

			Command.Parameters.Clear();
			PrepareParameters(names, values, item);

			Command.CommandText = "INSERT INTO " + MapTable(Alias.Objects.Category) + "(" + names + ") VALUES(" + values + ")";
			Command.ExecuteNonQuery();
		}
		public void Modify(int id, Dictionary<string, object> changes)
		{
			#region 判断是否出现环路
			if (changes.ContainsKey(Alias.Category.ParentID) && !Convert.IsDBNull(changes[Alias.Category.ParentID]))
			{
				Command.Parameters.Clear();
				Command.CommandText = "SELECT COUNT(*) FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.ID) + "=? AND (" + MapColumn(Alias.Category.InnerID) + "=" + id + " OR " + MapColumn(Alias.Category.ParentChain) + " LIKE '%." + id + "' OR " + MapColumn(Alias.Category.ParentChain) + " LIKE '%." + id + ".%')";
				Command.Parameters.Add("@" + Alias.Category.ID, MapType(Alias.Category.ID)).Value = changes[Alias.Category.ParentID];
				if (Convert.ToInt32(Command.ExecuteScalar()) > 0)
					changes.Remove(Alias.Category.ParentID);
			}
			#endregion

			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = id;

			#region 检查并设置RowLang
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.RowLang) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			string rowLang = Command.ExecuteScalar().ToString();
			if (!rowLang.Contains("|" + Lang + "|"))
				changes.Add(Alias.Category.RowLang, rowLang + Lang + "|");
			#endregion

			#region 设置ParentChain
			if (changes.ContainsKey(Alias.Category.ParentID))
			{
				Command.CommandText = "SELECT TOP 1 " + MapColumn(Alias.Category.ParentChain) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
				object oldParentChain = Command.ExecuteScalar();

				if (changes[Alias.Category.ParentID] == DBNull.Value)
				{
					changes[Alias.Category.ParentChain] = "0";
				}
				else
				{
					Command.Parameters.Clear();
					Command.Parameters.Add("@" + Alias.Category.ID, MapType(Alias.Category.ID));

					string parentChain = "";
					object parentID = changes[Alias.Category.ParentID];
					object innerID;
					while (!Convert.IsDBNull(parentID))
					{
						Command.CommandText = "SELECT TOP 1 " + MapColumn(Alias.Category.ParentID) + "," + MapColumn(Alias.Category.InnerID) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.ID) + "=?";
						Command.Parameters["@" + Alias.Category.ID].Value = parentID;
						DataReader = Command.ExecuteReader();
						if (!DataReader.HasRows)
						{
							DataReader.Close();
							break;
						}

						DataReader.Read();
						parentID = DataReader[0];
						innerID = DataReader[1];
						DataReader.Close();

						parentChain = "." + innerID + parentChain;
					}

					changes[Alias.Category.ParentChain] = "0" + parentChain;
				}

				string oldChain = oldParentChain + "." + id;
				string newChain = changes[Alias.Category.ParentChain] + "." + id;
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.ParentChain) + "='" + newChain + "' + Right(" + MapColumn(Alias.Category.ParentChain) + ",Len(" + MapColumn(Alias.Category.ParentChain) + ") - " + oldChain.Length + ") WHERE " + MapColumn(Alias.Category.ParentChain) + " LIKE '" + oldChain + "%'";
				Command.ExecuteNonQuery();
			}
			#endregion

			#region 推荐
			if (changes.ContainsKey(Alias.Category.IsRecommended))
			{
				if (Convert.ToBoolean(changes[Alias.Category.IsRecommended]))
				{
					Command.CommandText = "SELECT MAX(" + MapColumn(Alias.Category.Recommend) + ") + 1 FROM " + MapTable(Alias.Objects.Category);
					object recommend = Command.ExecuteScalar();
					if (recommend == DBNull.Value)
						changes[Alias.Category.Recommend] = 1;
					else
						changes[Alias.Category.Recommend] = recommend;
				}
				else
				{
					changes[Alias.Category.Recommend] = 0;
				}
				changes.Remove(Alias.Category.IsRecommended);
			}
			#endregion

			StringBuilder sql = new StringBuilder("UPDATE " + MapTable(Alias.Objects.Category) + " SET ");
			Command.Parameters.Clear();
			PrepareParameters(sql, changes);
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = id;

			sql.Append(" WHERE " + MapColumn(Alias.Category.InnerID) + "=?");

			#region 权限
			if (CheckRight)
			{
				Dictionary<string, string> user = Manager.User;
				if (user != null)
				{
					#region 管理员
					if (user["Namespace"] == "AliCMS.SystemModule")
					{
						if (Convert.ToBoolean(user["HasSelfLimitation"]))
						{
						}
					}
					#endregion
					#region 用户
					else
					{
					}
					#endregion
				}
			}
			#endregion

			Command.CommandText = sql.ToString();
			Command.ExecuteNonQuery();
		}
		public void Delete(int id)
		{
			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = id;

			StringBuilder sql = new StringBuilder("DELETE FROM " + MapTable(Alias.Objects.Category) + " WHERE (" + MapColumn(Alias.Category.InnerID) + "=? OR " + MapColumn(Alias.Category.ParentChain) + " LIKE '%." + id + "' OR " + MapColumn(Alias.Category.ParentChain) + " LIKE '%." + id + ".%')");

			#region 权限
			if (CheckRight)
			{
				Dictionary<string, string> user = Manager.User;
				if (user != null)
				{
					#region 管理员
					if (user["Namespace"] == "AliCMS.SystemModule")
					{
					}
					#endregion
					#region 用户
					else
					{
					}
					#endregion
				}
			}
			#endregion

			Command.CommandText = sql.ToString();
			Command.ExecuteNonQuery();
		}
		public void Move(int from, int to)
		{
			if (from == to)
				return;

			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID));

			#region 判断是否出现环路
			Command.CommandText = "SELECT COUNT(*) FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=? AND (" + MapColumn(Alias.Category.ParentChain) + " LIKE '%." + from + "' OR " + MapColumn(Alias.Category.ParentChain) + " LIKE '%." + from + ".%')";
			Command.Parameters["@" + Alias.Category.InnerID].Value = to;
			if (Convert.ToInt32(Command.ExecuteScalar()) > 0)
				return;
			#endregion

			#region 获取to的父菜单项ID（toParentID）和排序（toSort）
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.ParentID) + "," + MapColumn(Alias.Category.Sort) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			DataReader = Command.ExecuteReader();
			DataReader.Read();
			object toParentID = DataReader[0];
			int toSort = Convert.ToInt32(DataReader[1]);
			DataReader.Close();
			#endregion

			#region 获取from的排序值和InnerID
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.InnerID) + "," + MapColumn(Alias.Category.Sort) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			Command.Parameters["@" + Alias.Category.InnerID].Value = from;
			DataReader = Command.ExecuteReader();
			DataReader.Read();
			object fromInnerID = DataReader[0];
			int fromSort = Convert.ToInt32(DataReader[1]);
			DataReader.Close();
			#endregion

			#region 设置from的父菜单项为toParentID
			#region 设置ParentChain
			Command.CommandText = "SELECT TOP 1 " + MapColumn(Alias.Category.ParentChain) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			object oldParentChain = Command.ExecuteScalar();
			object newParentChain;

			if (toParentID == DBNull.Value)
			{
				newParentChain = "0";
			}
			else
			{
				Command.Parameters.Clear();
				Command.Parameters.Add("@" + Alias.Category.ID, MapType(Alias.Category.ID));

				string parentChain = "";
				object parentID = toParentID;
				object innerID;
				while (!Convert.IsDBNull(parentID))
				{
					Command.CommandText = "SELECT TOP 1 " + MapColumn(Alias.Category.ParentID) + "," + MapColumn(Alias.Category.InnerID) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.ID) + "=?";
					Command.Parameters["@" + Alias.Category.ID].Value = parentID;
					DataReader = Command.ExecuteReader();
					if (!DataReader.HasRows)
					{
						DataReader.Close();
						break;
					}

					DataReader.Read();
					parentID = DataReader[0];
					innerID = DataReader[1];
					DataReader.Close();

					parentChain = "." + innerID + parentChain;
				}

				newParentChain = "0" + parentChain;
			}

			string oldChain = oldParentChain + "." + fromInnerID;
			string newChain = newParentChain + "." + fromInnerID;
			Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.ParentChain) + "='" + newChain + "' + Right(" + MapColumn(Alias.Category.ParentChain) + ",Len(" + MapColumn(Alias.Category.ParentChain) + ") - " + oldChain.Length + ") WHERE " + MapColumn(Alias.Category.ParentChain) + " LIKE '" + oldChain + "%'";
			Command.ExecuteNonQuery();
			#endregion

			Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.ParentID) + "=?," + MapColumn(Alias.Category.ParentChain) + "='" + newParentChain + "' WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.ParentID, MapType(Alias.Category.ParentID)).Value = toParentID;
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = from;
			Command.ExecuteNonQuery();
			#endregion

			if (fromSort >= toSort)
			{
				#region to和大于他并且小于from的菜单排序都加1
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Sort) + "=" + MapColumn(Alias.Category.Sort) + "+1 WHERE " + MapColumn(Alias.Category.Sort) + ">=" + toSort/* + " AND " + MapColumn(Alias.Category.Sort) + " < " + fromSort*/;
				Command.ExecuteNonQuery();
				#endregion

				#region from的排序值设置为toSort
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Sort) + "=" + toSort + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
				Command.Parameters.Clear();
				Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = from;
				Command.ExecuteNonQuery();
				#endregion
			}
			/*
			else if (fromSort == toSort)
			{
				#region to和大于他的菜单排序都加1
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Sort) + "=" + MapColumn(Alias.Category.Sort) + "+1 WHERE " + MapColumn(Alias.Category.Sort) + ">=" + toSort;
				Command.ExecuteNonQuery();
				#endregion
			}
			*/
			else
			{
				#region to和小于他并且大于from的菜单排序都减1
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Sort) + "=" + MapColumn(Alias.Category.Sort) + "-1 WHERE " + MapColumn(Alias.Category.Sort) + "<=" + toSort + " AND " + MapColumn(Alias.Category.Sort) + " > " + fromSort;
				Command.ExecuteNonQuery();
				#endregion

				#region from的排序值设置为toSort
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Sort) + "=" + toSort + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
				Command.Parameters.Clear();
				Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID)).Value = from;
				Command.ExecuteNonQuery();
				#endregion
			}
		}
		public void MoveRecommend(int from, int to)
		{
			if (from == to)
				return;

			Command.Parameters.Clear();
			Command.Parameters.Add("@" + Alias.Category.InnerID, MapType(Alias.Category.InnerID));

			#region 获取to的推荐值
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.Recommend) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			Command.Parameters["@" + Alias.Category.InnerID].Value = to;
			int toRecommended = Convert.ToInt32(Command.ExecuteScalar());
			#endregion

			#region 获取from的推荐值
			Command.CommandText = "SELECT " + MapColumn(Alias.Category.Recommend) + " FROM " + MapTable(Alias.Objects.Category) + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
			Command.Parameters["@" + Alias.Category.InnerID].Value = from;
			int fromRecommended = Convert.ToInt32(Command.ExecuteScalar());
			#endregion

			if (fromRecommended > toRecommended)
			{
				#region to和大于他并且小于from的菜单推荐值都加1
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Recommend) + "=" + MapColumn(Alias.Category.Recommend) + "+1 WHERE " + MapColumn(Alias.Category.Recommend) + ">=" + toRecommended + " AND " + MapColumn(Alias.Category.Recommend) + " < " + fromRecommended;
				Command.ExecuteNonQuery();
				#endregion

				#region from的推荐值设置为toRecommended
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Recommend) + "=" + toRecommended + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
				Command.ExecuteNonQuery();
				#endregion
			}
			else if (fromRecommended == toRecommended)
			{
				#region to和大于他的菜单推荐值都加1
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Recommend) + "=" + MapColumn(Alias.Category.Recommend) + "+1 WHERE " + MapColumn(Alias.Category.Recommend) + ">=" + toRecommended;
				Command.ExecuteNonQuery();
				#endregion
			}
			else
			{
				#region to和小于他并且大于from的菜单推荐值都减1
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Recommend) + "=" + MapColumn(Alias.Category.Recommend) + "-1 WHERE " + MapColumn(Alias.Category.Recommend) + "<=" + toRecommended + " AND " + MapColumn(Alias.Category.Recommend) + " > " + fromRecommended;
				Command.ExecuteNonQuery();
				#endregion

				#region from的推荐值设置为toRecommended
				Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Category) + " SET " + MapColumn(Alias.Category.Recommend) + "=" + toRecommended + " WHERE " + MapColumn(Alias.Category.InnerID) + "=?";
				Command.ExecuteNonQuery();
				#endregion
			}
		}
		public DataTable List(int page, int pageSize, string condition, string sort)
		{
			DataTable result = new DataTable();
			result.Columns.Add(Alias.Category.ID, MapCSType(Alias.Category.ID));
			result.Columns.Add(Alias.Category.InnerID, MapCSType(Alias.Category.InnerID));
			result.Columns.Add(Alias.Category.Name, MapCSType(Alias.Category.Name));
			result.Columns.Add(Alias.Category.ParentID, MapCSType(Alias.Category.ParentID));
			result.Columns.Add(Alias.Category.ParentChain, MapCSType(Alias.Category.ParentChain));
			result.Columns.Add(Alias.Category.Picture, MapCSType(Alias.Category.Picture));
			result.Columns.Add(Alias.Category.Recommend, MapCSType(Alias.Category.Recommend));
			result.Columns.Add(Alias.Category.IsDisplay, MapCSType(Alias.Category.IsDisplay));
			result.Columns.Add(Alias.Category.Sort, MapCSType(Alias.Category.Sort));

			#region 条件
			Command.Parameters.Clear();
			StringBuilder where = new StringBuilder();
			if (!String.IsNullOrEmpty(condition))
			{
				Match match = null;
				where.Append(" WHERE " + Alias.Category.RowLang + " LIKE '%|" + Lang + "|%' AND ");
				where.Append("(" + condition + ")");

				#region IsRecommended
				match = Regex.Match(where.ToString(), @"(?<=(^|\s|\())" + Alias.Category.IsRecommended + @"\s?=\s?(?<v>true|false|0|1)(?=(\s|\)|$))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
				if (match.Success)
				{
					where.Remove(match.Index, match.Length);
					if (match.Groups["v"].Value.ToLower() == "true" || match.Groups["v"].Value == "0")
						where.Insert(match.Index, Alias.Category.Recommend + ">0");
					else
						where.Insert(match.Index, Alias.Category.Recommend + "=0");
				}
				#endregion

				ParseCondition(where, "");
			}
			else
			{
				where.Append(" WHERE " + MapColumn(Alias.Category.RowLang) + " LIKE '%|" + Lang + "|%'");
			}

			#region 权限
			if (CheckRight)
			{
				Dictionary<string, string> user = Manager.User;
				if (user != null)
				{
					#region 管理员
					if (user["Namespace"] == "AliCMS.SystemModule")
					{
					}
					#endregion
					#region 用户
					else
					{
					}
					#endregion
				}
			}
			#endregion
			#endregion

			#region 排序
			if (!String.IsNullOrEmpty(sort))
				sort = " ORDER BY " + ParseSort(sort, "");
			else
				sort = " ORDER BY " + MapColumn(Alias.Category.Sort) + " ASC";
			#endregion

			#region 构造SQL语句
			StringBuilder sql = new StringBuilder("SELECT");
			if (page > 0 && pageSize > 0)
				sql.Append(" TOP " + (page * pageSize));
			sql.Append(" " + MapColumn(Alias.Category.ID));
			sql.Append("," + MapColumn(Alias.Category.InnerID));
			sql.Append("," + MapColumn(Alias.Category.Name, Lang));
			sql.Append("," + MapColumn(Alias.Category.ParentID));
			sql.Append("," + MapColumn(Alias.Category.ParentChain));
			sql.Append("," + MapColumn(Alias.Category.Picture));
			sql.Append("," + MapColumn(Alias.Category.Recommend));
			sql.Append("," + MapColumn(Alias.Category.IsDisplay));
			sql.Append("," + MapColumn(Alias.Category.Sort));
			sql.Append(" FROM " + MapTable(Alias.Objects.Category));
			sql.Append(where + sort);
			Command.CommandText = sql.ToString();
			#endregion

			#region 执行SQL语句并获取记录
			int curIndex = 0, rowIndex = 0;
			int startIndex = (page - 1) * pageSize;
			DataReader = Command.ExecuteReader();
			while (DataReader.Read())
			{
				if (pageSize != -1)
				{
					if (curIndex < startIndex)
					{
						curIndex++;
						continue;
					}

					if (rowIndex == pageSize)
						break;
				}

				DataRow row = result.NewRow();
				int i = 0;
				row[Alias.Category.ID] = DataReader[i++];
				row[Alias.Category.InnerID] = DataReader[i++];
				row[Alias.Category.Name] = DataReader[i++];
				row[Alias.Category.ParentID] = DataReader[i] == DBNull.Value ? Guid.Empty : DataReader[i]; i++;
				row[Alias.Category.ParentChain] = DataReader[i++];
				row[Alias.Category.Picture] = DataReader[i++];
				row[Alias.Category.Recommend] = DataReader[i++];
				row[Alias.Category.IsDisplay] = DataReader[i++];
				row[Alias.Category.Sort] = DataReader[i++];
				result.Rows.Add(row);

				rowIndex++;
			}
			DataReader.Close();
			#endregion

			return result;
		}
		public int GetRecordCount(string condition)
		{
			#region 条件
			Command.Parameters.Clear();
			StringBuilder where = new StringBuilder();
			if (!String.IsNullOrEmpty(condition))
			{
				Match match = null;
				where.Append(" WHERE " + Alias.Category.RowLang + " LIKE '%|" + Lang + "|%' AND ");
				where.Append("(" + condition + ")");

				#region IsRecommended
				match = Regex.Match(where.ToString(), @"(?<=(^|\s|\())" + Alias.Category.IsRecommended + @"\s?=\s?(?<v>true|false|0|1)(?=(\s|\)|$))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
				if (match.Success)
				{
					where.Remove(match.Index, match.Length);
					if (match.Groups["v"].Value.ToLower() == "true" || match.Groups["v"].Value == "0")
						where.Insert(match.Index, Alias.Category.Recommend + ">0");
					else
						where.Insert(match.Index, Alias.Category.Recommend + "=0");
				}
				#endregion

				ParseCondition(where, "");
			}
			else
			{
				where.Append(" WHERE " + MapColumn(Alias.Category.RowLang) + " LIKE '%|" + Lang + "|%'");
			}

			#region 权限
			if (CheckRight)
			{
				Dictionary<string, string> user = Manager.User;
				if (user != null)
				{
					#region 管理员
					if (user["Namespace"] == "AliCMS.SystemModule")
					{
					}
					#endregion
					#region 用户
					else
					{
					}
					#endregion
				}
			}
			#endregion
			#endregion

			Command.CommandText = "SELECT COUNT(*) FROM " + MapTable(Alias.Objects.Category) + where;
			return Convert.ToInt32(Command.ExecuteScalar());
		}
	}
}
